function HandleProduce(gui, item, options) {
  this._gui = gui;
  this._item = item;
  this._options = options;
  this._popupBox = new PopupBox();
  this._popupBox2 = new PopupBox2();
  
  this._user = (options && options.user) ? options.user : null;
  this._date = (options && options.date) ? options.date : null;
  this._callbackFunc = (options && options.callbackFunc) ? options.callbackFunc : function() {};
  this._menu = (options && options.menu) ? options.menu : null;
  this._isfromcus = (options && options.isfromcus) ? options.isfromcus : null;
  this._isSupervise = (options && options.s) ? options.s : 0;

  this._createElements();
};

HandleProduce.prototype._createElements = function() {
  var options, model, state, transitions = [], a, button, _self = this;
  
  if (this._item.state < 0) {
    this._gui.statusDiv.appendChild(document.createTextNode('Closed'));
    return;
  }
  
  model = Manage.map()[this._item.pi_model];

  for (var i = 0, il = model.states.length; i < il; i++) {
    var s = model.states[i];
    if (this._item.state == s.id) {
      state = s;
      break;
    }
  }
 
// console.log(state)
  for (var i = 0, il = model.transitions.length; i < il; i++) {
    var t = model.transitions[i];
    if (this._item.state == t.source.id) {
      transitions[transitions.length] = t;
    }
  }
  options = MiscUtils.clone(this._options);
  options.transitions = transitions;
  options.callbackFunc = function() { _self._popupBox._close();
                                      _self._callbackFunc();
                                    };

  if (this._item.cancelled == 0) {
    if (this._item.isCustom == 1) {
      this._gui.statusDiv.appendChild(document.createTextNode(state.title + ' (' + ((this._item.customTask.length > 18) ? this._item.customTask.substring(0, 18) + '...' : this._item.customTask)));
      if (this._item.customTask.length > 18) {
        var a = document.createElement('a');
        a.appendChild(document.createTextNode('Info'));
        a.href = '#';
        a.onclick = function() { var div = document.createElement('div');
                                 var pos = DOMUtils.findPos(this);
                                 func1 = function() { _self._popupBox._close(); };
                                 div.appendChild(MiscUtils.span(_self._item.customTask));
                                 MiscUtils.dialog(_self._popupBox, pos, div, func1, null, null);
                                 return false;
                               };
        this._gui.statusDiv.appendChild(a);
      }
      this._gui.statusDiv.appendChild(document.createTextNode(' )'));
    } else {
      this._gui.statusDiv.appendChild(document.createTextNode(state.title));
    }

    a = document.createElement('a');
    a.style.fontSize = '10px';
    a.href = '#';
    a.appendChild(document.createTextNode('Log'));
    a.onclick = function() { var pos = DOMUtils.findPos(this);
                             var func1 = function() { _self._popupBox2._close(); };
                             var div = document.createElement('div');
                             MiscUtils.dialog(_self._popupBox2, DOMUtils.findPos(this), div, func1, null, {t: _self._item.title, width: 820, height: 50});
                             options.callbackFunc = function() { _self._popupBox2._close();
                                                                 _self._callbackFunc(pos);
                                                               };
                             new DialogHistory(div, _self._item, options);
                             return false;
                           };
    this._gui.statusDiv.appendChild(a);
    
    this._gui.deadlineDiv.style.color = (this._item.deadline && ((SimpleDate.toStamp(this._item.deadline) - SimpleDate.toStamp(this._date)) / 86400) < 0) ? '#ff0000' : '#000000';
    this._gui.deadlineDiv.appendChild(document.createTextNode((this._item.deadline) ? (((((SimpleDate.toStamp(this._item.deadline) - SimpleDate.toStamp(this._date)) / 86400) <= -1) || (((SimpleDate.toStamp(this._item.deadline) - SimpleDate.toStamp(this._date)) / 86400) >= 0)) ? parseInt((SimpleDate.toStamp(this._item.deadline) - SimpleDate.toStamp(this._date)) / 86400, 10) : '-1') : ''));
    
    
    if (this._item.deadline && this._user.type == '{{$smarty.const.USER_TYPE_MANAGER|escape:javascript}}') {
      a = document.createElement('a');
      a.style.fontSize = '10px';
      a.href = '#';
      a.appendChild(document.createTextNode('Chg'));
      a.onclick = function() { var context = this; var pos = DOMUtils.findPos(this);
                               _self._changeDate.call(_self, this, _self._item.deadline, function(sd) { _self._item.deadline = sd; new RequestUtils()._write('produce', [_self._item], [], function() { _self._callbackFunc();}, {php: Constant.FACTORY + '/customphp/mysqlwrite.php', pos: pos}); }, false);
                               return false;
                             };
      this._gui.deadlineDiv.appendChild(a);
    }
    
    if (this._user.type == '{{$smarty.const.USER_TYPE_MANAGER|escape:javascript}}') {
      if (!(this._item.completed == 1 || this._item.cancelled == 1)) {
        if (this._item.urgent == 1) {
          var img1 = document.createElement('img');
          img1.src = 'images/common/reverse.png';
          img1.style.cursor = 'pointer';
          img1.onclick = function() { _self._item.urgent = 0;
                                      var pos = DOMUtils.findPos(this);
                                      new RequestUtils()._write('produce', [_self._item], [], function() { _self._callbackFunc();}, {php: Constant.FACTORY + '/customphp/mysqlwrite.php', pos: pos});
                                    };
          this._gui.deadlineDiv.appendChild(img1);
        } else {
          var img = document.createElement('img');
          img.src = 'images/common/urgent.png';
          img.style.cursor = 'pointer';
          img.onclick = function() { _self._item.urgent = 1;
                                     var pos = DOMUtils.findPos(this);
                                     new RequestUtils()._write('produce', [_self._item], [], function() { _self._callbackFunc();}, {php: Constant.FACTORY + '/customphp/mysqlwrite.php', pos: pos});
                                   };
          this._gui.deadlineDiv.appendChild(img);
        }
      }
    }

    var count = 0;
    for (var i = 0, il = transitions.length; i < il; i++) {
      var t = transitions[i];
      if (t.validate(this._item, this._user, this._date, null)) {
        if (this._item.pi_model != Constant.MODEL_NAME_MAIL && this._item.pi_model != Constant.MODEL_NAME_MAIL_VIDEO) {
          button = DOMUtils.createInput(null, null, 'button', t.title, null);
          button._transition = t;
          button.onclick = function() { this.disabled = true;
                                        var pos = DOMUtils.findPos(this);
                                        if (parseInt((SimpleDate.toStamp(_self._item.deadline) - SimpleDate.toStamp(_self._date)) / 86400, 10) < 0) {
                                          var div = document.createElement('div');
                                          var transition = this._transition;
                                          var button = this;
                                          var func1 = function() { if (_self._item._text == '') {
                                                                     window.alert('NOT BE EMPTY');
                                                                   } else {
                                                                     transition.operate(pos, function() { _self._callbackFunc(pos); }, _self._item, _self._user, _self._date, {overdueReason: _self._item._text});
                                                                     _self._popupBox2._close();
                                                                   }
                                                                 };
                                          var func2 = function() { _self._popupBox2._close(); _self._callbackFunc(pos); };
                                          var func3 = function() { _self._callbackFunc(pos); };
                                          MiscUtils.dialog(_self._popupBox2, pos, div, func1, func2, {t: 'Overdue Reason', func: func3});
                                          new DialogInput(div, _self._item, { title: '' });
                                        } else {
                                          this._transition.operate(pos, function() { _self._callbackFunc(pos); }, _self._item, _self._user, _self._date, null);
                                        }
                                      };
          this._gui.buttonDiv.appendChild(button);
        }  else if (this._item.pi_model == Constant.MODEL_NAME_MAIL) {
          a = document.createElement('a');
          if (this._user.type == Constant.USER_TYPE_MANAGER_ASSISTANT) {
            a.href = '?p=pageasst&t=pagehistorymailinformation&m=' + JSON.stringify(this._menu) + '&opts=' + JSON.stringify({ p: this._item.id });
          } else if (this._user.type == Constant.USER_TYPE_MANAGER) {
            a.href = '?p=pagemanager&t=pagehistorymailinformation&m=null' + '&opts=' + JSON.stringify({ p: this._item.id });
          } else if (this._user.type == Constant.USER_TYPE_ADMIN) {
            a.href = '?p=pageadmin&t=pagehistorymailinformation&m=null' + '&opts=' + JSON.stringify({ p: this._item.id });
          }
          a.appendChild(document.createTextNode('Receive'));
          this._gui.buttonDiv.appendChild(a);
        } else if (this._item.pi_model == Constant.MODEL_NAME_MAIL_VIDEO) {
          a = document.createElement('a');
          if (this._user.type == Constant.USER_TYPE_MANAGER) {
            a.href = '?p=pagemanager&t=pagehistorytapesparcel&m=' + JSON.stringify(this._menu) + '&opts=' + JSON.stringify({ p: this._item.id });
          } else if (this._user.type == Constant.USER_TYPE_ADMIN) {
            a.href = '?p=pageadmin&t=pagehistorytapesparcel&m=' + JSON.stringify(this._menu) + '&opts=' + JSON.stringify({ p: this._item.id });
          }
          a.appendChild(document.createTextNode('Receive'));
          this._gui.buttonDiv.appendChild(a);
        }
		//else {
         // a = document.createElement('a');
        //  a.href = '?p=pageasst&t=pagehistorymailinformation&m=' + JSON.stringify(this._menu) + '&opts=' + JSON.stringify({ p: this._item.id });
         // a.appendChild(document.createTextNode('Receive'));
          //this._gui.buttonDiv.appendChild(a);
        //}
        count++;
      }
    }
    
    if (count == 0 && transitions.length != 0) {
      if (this._item.isCustom == 0) {
        if (this._item.pi_model == Constant.MODEL_NAME_USER_VIDEOGRAPHER && this._item.state == 6) {
          if (this._user.type == Constant.USER_TYPE_MANAGER_ASSISTANT || this._user.type == Constant.USER_TYPE_MANAGER || this._user.type == Constant.USER_TYPE_ADMIN) {
            var a = document.createElement('a');
            if (this._user.type == Constant.USER_TYPE_MANAGER_ASSISTANT) {
              a.href = '?p=pageasst&t=pageb1sendtape&m=' + JSON.stringify({a:1, b:1, c:1, d:14});
            } else if (this._user.type == Constant.USER_TYPE_MANAGER) {
              a.href = '?p=pagemanager&t=pageb1sendtape&m=' + JSON.stringify({a:1, b:2, c:14}) + '&s=' + JSON.stringify({s: 2});
            } else if (this._user.type == Constant.USER_TYPE_ADMIN) {
              a.href = '?p=pageadmin&t=pageb1sendtape&m=' + JSON.stringify({a:1, b:4, c:1});
            }
            a.appendChild(document.createTextNode(transitions[0].position));
            this._gui.buttonDiv.appendChild(a);
          } else {
            this._gui.buttonDiv.appendChild(document.createTextNode(Constant.USER_TYPE_MANAGER_ASSISTANT));
          }
        } else {
          if ((this._item.pi_model == Constant.MODEL_NAME_PRODUCT_COMMON && this._item.state == 16) || (this._item.pi_model == Constant.MODEL_NAME_PRODUCT_RETOUCH_MAGAZINE && this._item.state == 16) || (this._item.pi_model == Constant.MODEL_NAME_PRODUCT_4X6 && this._item.state == 10) || (this._item.pi_model == Constant.MODEL_NAME_PRODUCT_VIDEODVD && this._item.state == 5) || (this._item.pi_model == Constant.MODEL_NAME_PRODUCT_VIDEODVD_BLURAY && this._item.state == 5) || (this._item.pi_model == Constant.MODEL_NAME_PRODUCT_SLIDESHOW && this._item.state == 8) || (this._item.pi_model == Constant.MODEL_NAME_PRODUCT_SLIDESHOW_OL && this._item.state == 8)) {
            if (this._user.type == Constant.USER_TYPE_MANAGER_ASSISTANT || this._user.type == Constant.USER_TYPE_MANAGER || this._user.type == Constant.USER_TYPE_ADMIN) {
              var a = document.createElement('a');
              if (this._user.type == Constant.USER_TYPE_MANAGER_ASSISTANT) {
                a.href = '?p=pageasst&t=pageb1receiveproduct&m=' + JSON.stringify({a:1, b:1, c:1, d:16});
              } else if (this._user.type == Constant.USER_TYPE_MANAGER) {
                a.href = '?p=pagemanager&t=pageb1receiveproduct&m=' + JSON.stringify({a:1, b:2, c:16}) + '&s=' + JSON.stringify({s: 1});
              } else if (this._user.type == Constant.USER_TYPE_ADMIN) {
                a.href = '?p=pageadmin&t=pageb1receiveproduct&m=' + JSON.stringify({a:1, b:4, c:1});
              }
              a.appendChild(document.createTextNode('Receive'));
              this._gui.buttonDiv.appendChild(a);
            } else {
              this._gui.buttonDiv.appendChild(document.createTextNode(((transitions[0].position && transitions[0].position.charAt(0) == '#') ? ((transitions[0].position.substring(1) == 'Film Cutter') ? 'Video Editing' : transitions[0].position.substring(1)) : transitions[0].position) + ''));
            }
          } else if ((this._item.pi_model == Constant.MODEL_NAME_USER_VIDEOGRAPHER && this._item.state == 7)) {
            if (this._user.type == Constant.USER_TYPE_MANAGER) {
              var a = document.createElement('a');
              a.href = '?p=pagemanager&t=pageb1receivetape&m=' + JSON.stringify({a:1, b:2, c:2}) + '&s=' + JSON.stringify({s: 4});
              a.appendChild(document.createTextNode('Receive'));
              this._gui.buttonDiv.appendChild(a);
            } else if (this._user.type == Constant.USER_TYPE_ADMIN) {
              var a = document.createElement('a');
              a.href = '?p=pageadmin&t=pageb1receivetape&m=' + JSON.stringify({a:1, b:4, c:1}) ;
              a.appendChild(document.createTextNode('Receive'));
              this._gui.buttonDiv.appendChild(a);
            } else {
              this._gui.buttonDiv.appendChild(document.createTextNode(((transitions[0].position && transitions[0].position.charAt(0) == '#') ? ((transitions[0].position.substring(1) == 'Film Cutter') ? 'Video Editing' : transitions[0].position.substring(1)) : transitions[0].position) + ''));
            }
          } else {
            this._gui.buttonDiv.appendChild(document.createTextNode(((transitions[0].position && transitions[0].position.charAt(0) == '#') ? ((transitions[0].position.substring(1) == 'Film Cutter') ? 'Video Editing' : transitions[0].position.substring(1)) : transitions[0].position) + ''));
          }
        }
      } else {
        if (true) {
          this._gui.buttonDiv.appendChild(document.createTextNode(this._item.user_account));
        }
      }
    }
  } else {
    this._gui.statusDiv.appendChild(document.createTextNode('Cancelled'));
//    if (this._item.v2_events.length > 0) {
      a = document.createElement('a');
      a.style.fontSize = '10px';
      a.href = '#';
      a.appendChild(document.createTextNode('Log'));
      a.onclick = function() { var pos = DOMUtils.findPos(this);
                               var func1 = function() { _self._popupBox2._close(); };
                               var div = document.createElement('div');
                               MiscUtils.dialog(_self._popupBox2, DOMUtils.findPos(this), div, func1, null, {t: _self._item.title, width: 820, height: 50});
                               options.callbackFunc = function() { _self._popupBox2._close();
                                                                   _self._callbackFunc(pos);
                                                                 };
                               new DialogHistory(div, _self._item, options);
                               return false;
                             };
      this._gui.statusDiv.appendChild(a);
//    }
  }
  
  if (this._item.pi_model == Constant.MODEL_NAME_MAIL && this._item.state != 1) {
    a = document.createElement('a');
    if (this._user.type == Constant.USER_TYPE_MANAGER_ASSISTANT) {
      a.href = '?p=pageasst&t=pagehistorymailinformation&m=' + JSON.stringify(this._menu) + '&opts=' + JSON.stringify({ p: _self._item.id });
    } else if (this._user.type == Constant.USER_TYPE_MANAGER) {
      a.href = '?p=pagemanager&t=pagehistorymailinformation&m=' + JSON.stringify(this._menu) + '&opts=' + JSON.stringify({ p: _self._item.id });
    } else if (this._user.type == Constant.USER_TYPE_ADMIN) {
      a.href = '?p=pageadmin&t=pagehistorymailinformation&m=' + JSON.stringify(this._menu) + '&opts=' + JSON.stringify({ p: _self._item.id });
    }
    a.appendChild(document.createTextNode('Review'));
    this._gui.buttonDiv.appendChild(a);
  }
};

/* 
 * 修改日期1Change
 */
HandleProduce.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var div, _self = this;
  div = document.createElement('div');
  div.style.left = DOMUtils.findPos(label)[0] + 'px';
  div.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(div, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: div, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};
/* 
 * 修改日期2Select
 */
HandleProduce.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};
/* 
 * 修改日期3Show
 */
HandleProduce.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};
